<?
class Register extends TPage
{
	
			
	public function onInit($param)
    {	
			parent::onInit($param);
			if(!$this->IsPostBack)  // if the page is requested the first time
			{			}
			
			$this->initBirthDayObj();
    }
    
    public function initBirthDayObj()
    {
    	$this->DDLYear->DataSource=$this->getYear();
		$this->DDLYear->dataBind();
		 
        $this->DDLMonth->DataSource=$this->getMonth();
        $this->DDLMonth->dataBind();
		
		$this->DDLDay->DataSource=$this->getDay();
		$this->DDLDay->dataBind();
    }
    
    public function getMonth()
	{
	  $data = array('1'=>'Jan',
					'2'=>'Feb',
					'3'=>'Mar',
					'4'=>'Apr',
					'5'=>'May',
					'6'=>'Jun',
					'7'=>'July',
					'8'=>'Aug',
					'9'=>'Sep',
					'10'=>'Oct',
					'11'=>'Nov',
					'12'=>'Dec');		
		return $data;			
	}
	
	public function getDay()
	{
		$data = array();
		for($i = 1;$i <= 31; $i++)
		{
			array_push($data ,$i);
		}
		return $data;
	}
	
	public function getYear()
	{
		$data = array();
		for($i = 1950;$i <= 2005; $i++)
		{
			array_push($data ,$i);
		}
		return $data;
	}
    
    /**
     * Checks whether the username exists in the database.
     * This method responds to the OnServerValidate event of username's custom validator.
     * @param mixed event sender
     * @param mixed event parameter
     */
    public function checkEmail($sender,$param)
    {
        // valid if the username is not found in the database
        $param->IsValid = UserRecord::finder()->findByEmail($this->Email->Text)===null;
    }
 
    /**
     * Creates a new user account if all inputs are valid.
     * This method responds to the OnClick event of the "create" button.
     * @param mixed event sender
     * @param mixed event parameter
     */
    public function createButtonClicked($sender,$param)
    {
        if($this->IsValid)  // when all validations succeed
        {
            // populates a UserRecord object with user inputs
            $userRecord = new UserRecord;
            $userRecord->username = $this->Username->Text;
            $userRecord->password = $this->Password->Text;
            $userRecord->email = $this->Email->Text;
            $userRecord->role = 0;
            $userRecord->first_name = $this->FirstName->Text;
            $userRecord->last_name = $this->LastName->Text;
 			$userRecord->avatarPath = null;
 			$userRecord->isPrivate = $this->isPrivateChk->Checked;
 			$userRecord->userStatus = "Changed your status here.. ";
 			$userRecord->birthDate =  $this->DDLYear->SelectedItem->Text."-".
						$this->DDLMonth->SelectedItem->Value."-".
						$this->DDLDay->SelectedItem->Text;
			
			$sex = -1;
			if($this->radMale->Checked)
				$sex = 1;
			else if($this->radFemale->Checked)
				$sex = 0;		
		   	$userRecord->sex = $sex;
		   	
            // saves to the database via Active Record mechanism
            $userRecord->save();
 
            $url = $this->Service->constructUrl('users.LoginUser',array('success_id' => 200,'email' => $userRecord->email));
            $this->Response->redirect($url);
        }
        

    }
    

	
}

?>